<?php 
//require_once dirname(__FILE__).'/../ini.php';
//include_once ROOTDIR.'/common/database.php';
include 'Demploi.php';
class Residence{
	private $idResidence;
	private $membres;
	private $nom;
	private $ville;
	private $pagePerso;
	private $adresse;
	private $codePostal;
	private $grilles;
	private $idMedia;
	

	
	private $offre_emploi;
	private $demande_emploi;
	private $activites;
	private $infos;
	private $bonPlan;
	
	private $groupe;
	
	
	function __construct($idResidence){
	 $this->idResidence=$idResidence;
	 $query="select * from residence where id_residence='$idResidence'";
	 $result=runQuery($query);
	 $result=mysql_fetch_assoc($result);
	 $this->idResidence=$result['id_residence'];
	 $this->nom=$result['nom_residence'];
	 $this->ville=$result['ville'];
	 $this->adresse=$result['adresse'];
	 $this->codePostal=$result['departement'];
	}
	function __destruct(){
	 }
	public  function getMembres(){
		return $this->membres;
	}
	public function getNom(){return $this->nom;}
	public function getVille(){return $this->ville;}
	public function getPagePerso(){return $this->pagePerso;}
	public function getAdresse(){return $this->adresse;}
	public function getCodePostal(){return $this->codePostal;}
	public function getIdResidence(){return $this->idResidence;}
	public function getGrille(){}
	public function getGroupe(){ return $this->groupe;}
	public function getIdMedia(){return $this->idMedia;}
	public function getOffreEmploi(){return $this->offre_emploi;}
	public function getDemandeEmploi(){return $this->demande_emploi;}
	public function getActivites(){return $this->activites;}
	public function getInfos(){return $this->infos;}
	public function getBonPlan(){return $this->bonPlan;}
	
// setters
	public function addMembre(){
		
	}
	public function setNom(){
	
	}
	public function setVille(){
		
	}
	public function setPagePerso(){
		
	}
	public function setAdresse(){
		
	}
	public function setCodePostal(){
		
	}
	public function setGrille(){
		
	}
	public function setIdMedia(){
		
	}
	
//set all the demand ids for a residence
	public function setDemandeEmploi(){
		$query="SELECT id_message FROM `message_types` join messages on 
				`idmessage_types`=message_types_idmessage_types join
				membres on membres_id_membre=id_membre join 
				residence on residence_id_residence=id_residence
				where (id_residence='".$this->idResidence."' OR etendue>1) and nom_message_type='demande_emploi' order by date_mess DESC";
		$result=runQuery($query);
		$idsdemandeEmploi=array();
		$i=0;
		while ($rows=mysql_fetch_assoc($result)){
			array_unshift($idsdemandeEmploi, $rows['id_message']);
			$i++;
		}
	$this->demande_emploi=$idsdemandeEmploi;
		
	}
//set all the offer ids for a residence
	public function setOffreEmloi(){
		$query="SELECT id_message FROM `message_types` join messages on 
				`idmessage_types`=message_types_idmessage_types join
				membres on membres_id_membre=id_membre join 
				residence on residence_id_residence=id_residence
				where (id_residence='".$this->idResidence."' OR etendue>1) and nom_message_type='offre_emploi' order by date_mess DESC";
		$result=runQuery($query);
		$idsOffreEmploi=array();
		$i=0;
		while ($rows=mysql_fetch_assoc($result)){
			array_unshift($idsOffreEmploi, $rows['id_message']);
			$i++;
		}
		
	$this->offre_emploi=$idsOffreEmploi;
	}

	public function setActivites(){
		$query="SELECT id_message FROM `message_types` join messages on 
				`idmessage_types`=message_types_idmessage_types join
				membres on membres_id_membre=id_membre join 
				residence on residence_id_residence=id_residence
				where (id_residence='".$this->idResidence."' OR etendue>1) and nom_message_type='activites' order by date_mess DESC";
		$result=runQuery($query);
		$idsActivites=array();
		$i=0;
		while ($rows=mysql_fetch_assoc($result)){
			array_unshift($idsActivites, $rows['id_message']);
			$i++;
		}
		
	$this->activites=$idsActivites;
	}
	
	public function setInfos(){
		$query="SELECT id_message FROM `message_types` join messages on 
				`idmessage_types`=message_types_idmessage_types join
				membres on membres_id_membre=id_membre join 
				residence on residence_id_residence=id_residence
				where (id_residence='".$this->idResidence."' OR etendue>1) and nom_message_type='infos' order by date_mess DESC";
		$result=runQuery($query);
		$idsinfos=array();
		$i=0;
		while ($rows=mysql_fetch_assoc($result)){
			array_unshift($idsinfos, $rows['id_message']);
			$i++;
		}
		
	$this->infos=$idsinfos;
	}
	
	public function setBonPlan(){
		$query="SELECT id_message FROM `message_types` join messages on 
				`idmessage_types`=message_types_idmessage_types join
				membres on membres_id_membre=id_membre join 
				residence on residence_id_residence=id_residence
				where (id_residence='".$this->idResidence."' OR etendue>1) and nom_message_type='offre_emploi' order by date_mess DESC";
		$result=runQuery($query);
		$idsBonPlan=array();
		$i=0;
		while ($rows=mysql_fetch_assoc($result)){
			array_unshift($idsBonPlan, $rows['id_message']);
			$i++;
		}
		
	$this->bonPlan=$idsBonPlan;
	}
	
	public function setGroupe(){
		//selection should take only groups for a particular residence 
		$query="select id_groupe from groupe";
		
		$result=runQuery($query);
		$grpeList=array();
		while($rows=mysql_fetch_array($result,MYSQL_ASSOC)){
			array_unshift($grpeList, $rows['id_groupe']);
		}
	 $this->groupe=$grpeList;
	} 
// others

	//get all the paragraph for a home page of a particular residence
	public function getParagraph(){
		$paragraphs="";
		$query="select para1 ,para2,para3,para4 from residence where id_residence='".$this->idResidence."'" ;
		$result=runQuery($query);
		$row=mysql_fetch_assoc($result);
		$paragraphs .=$row['para1']."<br>";
		$paragraphs .=$row['para2']."<br>";
		$paragraphs .=$row['para3']."<br>";
		$paragraphs .=$row['para4']."<br>";
		
		return $paragraphs;
	}
	
//creation de groupe
	public function createGroupe($groupeName){
		$query="insert into groupe (`nom_groupe`) values('$groupeName')";
		$result=runQuery($query);
	if($result==1){
			$this->groupe=array_unshift($this->groupe, mysql_insert_id());
		}
	}
	function __toString(){
		
	}
}